package com.gilight.base.controller;

import com.gilight.base.admin.categor.AdminCategoryService;
import com.gilight.base.admin.product.AdminProductService;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.dto.goods.AdminSpuDTO;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.service.SysZzjgService;
import com.gilight.base.util.MapUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * @author liken
 * 商品管理控制类
 */
@RestController
@RequestMapping("/web/admin-category")
public class AdminCategoryProductController {

    @Autowired
    private AdminCategoryService adminCategoryService;
    @Autowired
    private SysUserZzjgService sysUserZzjgService;
    /**
     * 后台低频接口，无需缓存，用于选择商品，需要

     * @return
     */
    @ApiOperation(value = "获取二级类目树")
    @RequestMapping(value = "/category-second-level-tree", method = {RequestMethod.GET})
    public ResultEntity categorySecondLevelTree() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminCategoryService.categorySecondLevelTree());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }

    @ApiOperation(value = "获取类目树")
    @RequestMapping(value = "/category-tree", method = {RequestMethod.GET})
    public ResultEntity categoryTree(AdminSpuDTO adminSpuDTO, Long adminId,Long zzjgId) {
        ResultEntity resultEntity = new ResultEntity();
        if(zzjgId == null){
            List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
            if(zzjgsUser!=null&&zzjgsUser.size()>0) {
                zzjgId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
            }
        }

        try {
            resultEntity.setData(adminCategoryService.categoryThreeTree(zzjgId));
        } catch (ServiceException e) {
            e.printStackTrace();
        };
        return resultEntity;
    }

    /**
     * 创建
     * @return
     */
    @ApiOperation(value = "创建")
    @RequestMapping(value = "/create")
    public ResultEntity create(
            String title,
            Long parentId,
            String iconUrl,
            String picUrl,
            Long zzjgId,
            @RequestParam(required = false,defaultValue = "-999") Integer orderc,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if(zzjgId == null){
                List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
                zzjgId = 1L;
                if(zzjgsUser!=null&&zzjgsUser.size()>0) {
                    zzjgId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
                }
            }

            resultEntity.setData(adminCategoryService.create(title,parentId,iconUrl,picUrl,adminId,zzjgId,orderc));
        } catch (ServiceException e) {
            e.printStackTrace();
        };
        return resultEntity;
    }




    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete")
    public ResultEntity delete(
            Long id,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminCategoryService.delete(id,adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/update")
    public ResultEntity update(
            Long id,
            String title,
            Long parentId,
            String iconUrl,
            String picUrl,
            @RequestParam(required = false,defaultValue = "-999") Integer orderc,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminCategoryService.update(id,title,parentId,iconUrl,picUrl,adminId,orderc));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }

    @ApiOperation(value = "所有")
    @RequestMapping(value = "/list")
    public ResultEntity  list(
            Long id,
            String title,
            Integer level,
            Long parentId,
            Integer page,
            Integer limit,
            Long adminId,
            Long zzjgId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if(zzjgId == null){
                List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
                if(zzjgsUser!=null&&zzjgsUser.size()>0) {
                    zzjgId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
                }
            }
            resultEntity.setData(adminCategoryService.list(id,title,level,parentId,page,limit,adminId,zzjgId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
}
